import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';
dayjs.locale('zh-cn'); // 全局使用简体中文
let formate24Time = (value) => {
  let now = new Date(value);
  let time = now.toLocaleString('chinese', {
    hour12: false
  })
  return time.slice(10, 15)
}
let formate24TimeAll = (value) => {
  let now = new Date(value);
  let time = now.toLocaleString('chinese', {
    hour12: false
  })
  return time.slice(0, 15)
}
let formatDateTime = (value, formatter = 'YYYY-MM-DD HH:mm') => {
  if (!value) return '';
  return dayjs(value).format(formatter);
};
let formatDate = value => {
  if (!value) return '';
  return dayjs(value).format('YYYY-MM-DD');
};
let formatTime = value => {
  if (!value) return '';
  return dayjs(value).format('YYYY/MM/DD');
};
let formatYearMonth = value => {
  if (!value) return '';
  return dayjs(value).format('YYYY-MM');
};
let formatTimestamp = value => {
  if (!value) return '';
  return dayjs(value).valueOf();
};

let desensitization = phone => {
  if (!phone) return '';
  let pat = /(\d{3})\d*/;
  return phone.replace(pat, '$1********');
};

let timeStamp = value => {
  if (!value) return '';
  return dayjs(value).valueOf()
};

let fillZero = value => {
  const valueNum = Number(value);
  if (Number.isInteger(valueNum) && valueNum < 10 && valueNum > 0) {
    return `0${valueNum}`;
  } else {
    return value;
  }
};
let formattype = value => {
  let types = [{
      value: 'proprietor',
      text: '业主',
    },
    {
      value: 'family',
      text: '家属',
    },
    {
      value: 'tenant',
      text: '租户',
    },
  ];
  let str = '';
  types.forEach(ele => {
    if (ele.value === value) {
      str = ele.text;
    }
  });
  return str === '' ? null : str;
};
let format = (key, arr) => {
  let str, attr1, attr2;
  if (arr.length > 0) {
    [attr1, attr2] = Object.keys(arr[0]);
  }
  arr.forEach(ele => {
    if (ele[attr1] + '' === key + '') {
      str = ele[attr2];
    }
  });
  return str === '' ? '' : str;
};
let formatArr = arr => {
  // console.log(arr);
  arr = '请选择,' + arr;
  return arr.split(',');
};
/**
 * 格式化日期函数
 * @param date {Date|Date String} [需要格式化的日期]
 * @param frm {String} [格式(如：yyyy-MM-dd hh:mm:ss)]
 * @return 格式化后的日期
 */
let _formatDate = (date, fmt) => {
  fmt = fmt ? fmt : 'yyyy-MM-dd hh:mm:ss';
  if (!date || !fmt) {
    return false;
  }

  let theDate = new Date(date);
  let o = {
    'M+': theDate.getMonth() + 1, // 月份
    'd+': theDate.getDate(), // 日
    'h+': theDate.getHours(), // 小时
    'm+': theDate.getMinutes(), // 分
    's+': theDate.getSeconds(), // 秒
    'q+': Math.floor((theDate.getMonth() + 3) / 3), // 季度
    S: theDate.getMilliseconds(), // 毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (theDate.getFullYear() + '').substr(4 - RegExp.$1.length),
    );
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ?
        o[k] :
        ('00' + o[k]).substr(('' + o[k]).length),
      );
    }
  }
  return fmt;
};
// 获取上一个月
let preMonth = (value) => {
  if (!value) return '';
  let year = dayjs(value).year() //获取当前日期的年份
  let month = dayjs(value).month() //获取当前日期的月份
  let days = new Date(year, month, 0)
  days = days.getDate() //获取当前日期中月的天数
  let year2 = year
  let month2 = parseInt(month) - 1
  if (month2 == 0) {
    year2 = parseInt(year2) - 1
    month2 = 12
  }
  let days2 = new Date(year2, month2, 0)
  days2 = days2.getDate()
  if (month2 < 10) {
    month2 = '0' + month2
  }
  let t2 = year2 + '-' + month2
  return t2
};

// 获取下一个月
let nextMonth = (value) => {
  if (!value) return '';
  let year = dayjs(value).year() //获取当前日期的年份
  let month = dayjs(value).month() //获取当前日期的月份
  let days = new Date(year, month, 0)
  days = days.getDate() //获取当前日期中月的天数
  var year2 = year
  var month2 = parseInt(month) + 1
  if (month2 == 13) {
    year2 = parseInt(year2) + 1
    month2 = 1
  }
  var days2 = new Date(year2, month2, 0)
  days2 = days2.getDate()
  if (month2 < 10) {
    month2 = '0' + month2
  }

  var t2 = year2 + '-' + month2
  return t2
};

let rangeFn = (value) => {
  if (!value) return '';
  n = n.toString()
  return n[1] ? n : '0' + n
};

let valueNUll = (value) => {
  if (!value) return '无';
  return value
};

let twoFix = (num) => {
  num = Number(num);
  let isInt = Number.isInteger(num);
  return isInt ? num : Number(num.toFixed(2));
};

let formatMoney = (s, type) => {
  s = s + ''
  let aa = false;
  if (s.substring(0, 1) == '-') {
    aa = true;
    s = s.substring(1);
  }
  if (/[^0-9\.]/.test(s)) return '0';
  if (s == null || s == '') return '0';
  s = s.toString().replace(/^(\d*)$/, '$1.');
  s = (s + '00').replace(/(\d*\.\d\d)\d*/, '$1');
  s = s.replace('.', ',');
  var re = /(\d)(\d{3},)/;
  while (re.test(s)) s = s.replace(re, '$1,$2');
  s = s.replace(/,(\d\d)$/, '.$1');
  if (type == 0) {
    // 不带小数位(默认是有小数位)
    var a = s.split('.');
    if (a[1] == '00') {
      s = a[0];
    }
  }
  if (aa) {
    return '¥-' + s;
  }
  return '¥' + s;
};

let moneyFilter = (s, type) => {
  if (s == null || s == undefined) return '-';
  s = s.toString();
  let negative = false;
  // è´Ÿæ•°
  if (s.slice(0, 1) === '-') {
    negative = true;
    s = s.slice(1);
  }
  if (s.slice(0, 1) === '+') {
    s = s.slice(1);
  }
  if (/[^0-9\.]/.test(s)) return '0';
  if (s == null || s == '') return '0';
  s = s.toString().replace(/^(\d*)$/, '$1.');
  s = (s + '00').replace(/(\d*\.\d\d)\d*/, '$1');
  s = s.replace('.', ',');
  var re = /(\d)(\d{3},)/;
  while (re.test(s)) s = s.replace(re, '$1,$2');
  s = s.replace(/,(\d\d)$/, '.$1');
  if (type == 0) {
    // ä¸å¸¦å°æ•°ä½(é»˜è®¤æ˜¯æœ‰å°æ•°ä½)
    var a = s.split('.');
    if (a[1] == '00') {
      s = a[0];
    }
  }
  return `¥${negative ? '-' : ''}${s}`;
};

let areaFilter = (s, type) => {
  if (s == null || s == undefined) return '-';
  s = s.toString();
  let negative = false;
  // è´Ÿæ•°
  if (s.slice(0, 1) === '-') {
    negative = true;
    s = s.slice(1);
  }
  if (s.slice(0, 1) === '+') {
    s = s.slice(1);
  }
  if (/[^0-9\.]/.test(s)) return '0';
  if (s == null || s == '') return '0';
  s = s.toString().replace(/^(\d*)$/, '$1.');
  s = (s + '00').replace(/(\d*\.\d\d)\d*/, '$1');
  s = s.replace('.', ',');
  var re = /(\d)(\d{3},)/;
  while (re.test(s)) s = s.replace(re, '$1,$2');
  s = s.replace(/,(\d\d)$/, '.$1');
  if (type == 0) {
    // ä¸å¸¦å°æ•°ä½(é»˜è®¤æ˜¯æœ‰å°æ•°ä½)
    var a = s.split('.');
    if (a[1] == '00') {
      s = a[0];
    }
  }
  return `${negative ? '-' : ''}${s}`;
};

let moneyFilterNoUnit = (s, type) => {
  if (s == "--" || s == "计划外") {
    return s;
  }
  if (s == null || s == undefined) return '-';
  s = s.toString();
  let negative = false;
  // è´Ÿæ•°
  if (s.slice(0, 1) === '-') {
    negative = true;
    s = s.slice(1);
  }
  if (s.slice(0, 1) === '+') {
    s = s.slice(1);
  }
  if (/[^0-9\.]/.test(s)) return '0';
  if (s == null || s == '') return '0';
  s = s.toString().replace(/^(\d*)$/, '$1.');
  s = (s + '00').replace(/(\d*\.\d\d)\d*/, '$1');
  s = s.replace('.', ',');
  var re = /(\d)(\d{3},)/;
  while (re.test(s)) s = s.replace(re, '$1,$2');
  s = s.replace(/,(\d\d)$/, '.$1');
  if (type == 0) {
    // ä¸å¸¦å°æ•°ä½(é»˜è®¤æ˜¯æœ‰å°æ•°ä½)
    var a = s.split('.');
    if (a[1] == '00') {
      s = a[0];
    }
  }
  return `${negative ? '-' : ''}${s}`;
};

//千分制
let fmoneyThousand = (s, n) => {
  if (s < 0) {
    var oldVal = s
    s = Math.abs(s)
  }
  n = n > 0 && n <= 20 ? n : 2;
  s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + '';
  var l = s.split('.')[0].split('').reverse(),
    r = s.split('.')[1],
    t = '';
  for (let i = 0; i < l.length; i++) {
    t += l[i] + ((i + 1) % 3 == 0 && i + 1 != l.length ? ',' : '');
  }
  let val = t.split('').reverse().join('') + '.' + r;
  return oldVal < 0 ? "-" + val : val
}
//乘法 
const accMul = function (arg1, arg2 = 100) {
  if (arg1 == "--" || arg1 == "计划外") {
    return arg1;
  }
  if (arg1 == null || arg2 == null) {
    return 0;
  }
  var n1, n2;
  var r1, r2; // 小数位数
  try {
    r1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  n1 = Number(arg1.toString().replace(".", ""));
  n2 = Number(arg2.toString().replace(".", ""));
  return n1 * n2 / Math.pow(10, r1 + r2);
}
const ellipsis = function (value, len) {
  if (!value) return ''
  if (value.length > len) {
    return value.slice(0, len) + '...'
  }
  return value
}
export default {
  formate24TimeAll,
  formate24Time,
  formatDateTime,
  formatDate,
  formatTime,
  _formatDate,
  formatYearMonth,
  formatTimestamp,
  desensitization,
  timeStamp,
  fillZero,
  formattype,
  format,
  formatArr,
  preMonth,
  preMonth,
  rangeFn,
  valueNUll,
  twoFix,
  formatMoney,
  moneyFilter,
  areaFilter,
  fmoneyThousand,
  moneyFilterNoUnit,
  accMul,
  ellipsis
};
